<?PHP
function toArray($strParams)
{
	$temp_query_parts = explode(' ', $strParams);  
	foreach($temp_query_parts as $q)
	{  
		$temp = explode("=", $q);
		if (count($temp) < 2) continue;  
		$key = $temp[0];  
		$value = str_replace(array('"',"'"), array('',''), $temp[1]);  
		$query_parms[$key] = $value;  
	}
	return $query_parms;
}

class Parser extends xmlBase
{
	var $path;
	var $current = array();

	var $page = "";

	function Parser($path = "./")
	{
		$this->path = $path;
		parent::xmlBase(true);
	}
	function make($page)
	{
		$file = "$this->path./$page";
		$this->parseFile($file);

		return $this->page;
	}
	function _cdata($parser, $data)
	{
		$current = end($this->current);
		if ( isset($current->text) )
		{
			//$data = trim(str_replace(array("\t","\n","\r",'  '), ' ', $current->text.' '.$data));
			$current->text .= $data;
		}
		else if ( !HtmlWriter::$renderLineBreaks )
		{
			$current->addControl(new htmlText($data));
		}
		else
		{
			$trimmedData = str_replace(array("\n", "\r\n", "\t"), '', trim($data));
			if ( !empty($trimmedData) )
			{
				$current->addControl(new htmlText($data));
			}
		}
	}
	function _tagOpen($parser, $tagName, $attrs = array())
	{
		if ( !class_exists($tagName) )
		{
			die ("Class: $tagName does not defined");
		}
		$object = new $tagName($attrs);

		$parent = end($this->current);
		$parent->addControl($object);
		if ( isset($attrs['id']) )
		{
			$this->page->$attrs['id'] = $object;
		}
		if ( isset($attrs['actionclick']) )
		{
			$object->addClick(new EventHandler($this->page, $attrs['actionclick']));
		}
		if ( isset($attrs['actionchange']) )
		{
			$object->addChange(new EventHandler($this->page, $attrs['actionchange']));
		}

		array_push($this->current, $object);

		//echo "<$tagName";
		//foreach ($attrs as $key=>$value)
		//{
		//	echo " $key=\"$value\"";
		//}
		//echo ">";
	}
	function _tagClose($parser, $tagName)
	{
		//echo "</$tagName>";
		$object = array_pop($this->current);
		$object->setPage($this->page);
		$object->OnInit($this->page, array());
	}
	function _pi($parser, $target, $data)
	{
		switch ($target)
		{
			case 'code':
				$info = toArray($data);
				require_once($info['file']);

				if ( class_exists($info['class']) )
				{
					$this->page = new $info['class'];
				}
				else
				{
					die ( "Class: {$info['class']} does not defined");
				}

				if ( isset($info['template']) )
				{
					$templateParser = new Parser();
					$this->template = $templateParser->make($info['template']);
					$this->page->template = $this->template;
				}

				array_push($this->current, $this->page);
				$obj = $this->page;
			break;
			case 'php':
				$obj = $this->page;
				ob_start();
				eval($data);
				$html = ob_get_contents();
				ob_clean();
				$current = end($this->current);
				$current->addControl(new htmlText($html));
				//echo $html;
			break;
			case 'ret':
				$obj = $this->page;
				$html = eval("return $data;");
				$current = end($this->current);
				$current->addControl(new htmlText($html));
				//echo eval("return $data;");
			break;
		}

	}
}

?>